﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ECCSystem.CheckClass;
using ECCSystem.BizRule;
using System.Diagnostics;
using System.Net.NetworkInformation;
using ECCSystem.UserControls;
using System.Net.Sockets;
using System.Net;
namespace ECCSystem
{
	public partial class frmRule : Form
	{
		string strUserName = "";
		string strUserID = "";
		string strUserIP = "";
		int intMode = 0;
		string strCaption = null;
		ArrayList objArrayList = null;
		public frmRule()
		{
			InitializeComponent();
		}

		public frmRule(ArrayList objDate)
		{
			objArrayList = objDate;
			InitializeComponent();
		}

		public frmRule(string vDate)
		{
			strCaption = vDate;
			InitializeComponent();
		}

		private void BindCmb(DataSet ds)
		{

			if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
			{
				DataTable dt = ds.Tables[0];
				DataRow dr = dt.NewRow();
				dr[0] = System.DBNull.Value;
				dr[1] = " ";
				dt.Rows.InsertAt(dr, 0);
				cmbSearchPerson.ValueMember = "ID";
				cmbSearchPerson.DisplayMember = "UserName";
				cmbSearchPerson.DataSource = dt;

				DataTable dtTmp = new DataTable();
				dtTmp = dt.Clone();
				dtTmp = dt.Copy();
				cmbCreatePerson.ValueMember = "ID";
				cmbCreatePerson.DisplayMember = "UserName";
				cmbCreatePerson.DataSource = dtTmp;
			}
		}

		private void frmRule_Load(object sender, EventArgs e)
		{
			ClsUserMaintenance objClsUserMaintenance = new ClsUserMaintenance();

			DataSet ds = (DataSet)objClsUserMaintenance.GetList();
			BindCmb(ds);
			BindData();
			if (objArrayList != null)
			{
				for (int i = 0; i < cklbKeyName.Items.Count; i++)
				{
					DataRowView objDRV = (DataRowView)cklbKeyName.Items[i];
					if (objArrayList.Contains(objDRV.Row["EmailContentKeyID"].ToString().Trim()))
					{
						cklbKeyName.SetItemChecked(i, true);
						cklbTmp.SetItemChecked(i, true);
					}
					else
					{
						cklbKeyName.SetItemChecked(i, false);
						cklbTmp.SetItemChecked(i, false);
					}
				}
				txtOverTimeUser.Text = "";
				txtOverTimeUser.Tag = "";
				txtContent.Text = "";
				txtTitle.Text = "";
				this.dtvUser.Enabled = false;
				btnUpdata.Enabled = false;
				btnAdd.Enabled = false;
				btnSave.Enabled = true;
				btnDelete.Enabled = false;
				btnCancel.Enabled = false;
				intMode = 0;
			}

			if (strCaption != null)
			{
				for (int i = 0; i < dtvUser.Rows.Count; i++)
				{
					if (dtvUser.Rows[i].Cells["Rule_caption"].Value.ToString() == strCaption)
					{
						DataGridViewCellEventArgs objDataGridViewCellEventArgs = new DataGridViewCellEventArgs(0,i);
						dtvUser_CellClick(null, objDataGridViewCellEventArgs);
						//txtOverTimeUser.Text = Convert.ToString(dtvUser.Rows[i].Cells["Rule_Member"].Value);
						//txtOverTimeUser.Tag = Convert.ToString(dtvUser.Rows[i].Cells["Rule_MemberID"].Value);
						//txtTitle.Text = Convert.ToString(dtvUser.Rows[i].Cells["Rule_caption"].Value);
						//txtContent.Text = Convert.ToString(dtvUser.Rows[i].Cells["Rule_content"].Value);
						//cmbCreatePerson.SelectedValue = dtvUser.Rows[i].Cells["Rule_CreatePerson"].Value;
						//if (dtvUser.Rows[i].Cells["Rule_CreateDate"].Value != System.DBNull.Value)
						//{
						//    dtpCreateDate.Value = dtvUser.Rows[i].Cells["Rule_CreateDate"].Value;
						//}
						break;
					}
				}
			}

			if (ClsCommon.gStrRight == "CSR" || ClsCommon.gStrRight == "SV")
			{
				btnUpdata.Enabled = false;
				btnAdd.Enabled = false;
				btnSave.Enabled = false;
				btnDelete.Enabled = false;
				btnCancel.Enabled = false;
			}
		}

		private void BindCheckView()
		{
			ClsRule objClsRule = new ClsRule();

			DataSet dsTmp = (DataSet)objClsRule.GetEmailTempletList();

			if (dsTmp.Tables.Count > 0 && dsTmp.Tables[0].Rows.Count > 0)
			{
				//DataRow objDataRow = dsTmp.Tables[0].NewRow();
				//objDataRow.ItemArray = new object[] {System.DBNull.Value,System.DBNull.Value, 
				//                                    " ",System.DBNull.Value,System.DBNull.Value,
				//                                    System.DBNull.Value,System.DBNull.Value,
				//                                    System.DBNull.Value,System.DBNull.Value,
				//                                    System.DBNull.Value,System.DBNull.Value,
				//                                    System.DBNull.Value,System.DBNull.Value,
				//                                    System.DBNull.Value,System.DBNull.Value,
				//                                    System.DBNull.Value,System.DBNull.Value};


				cklbKeyName.DataSource = dsTmp.Tables[0];
				cklbKeyName.DisplayMember = "EmailTemplet_Name";
				cklbKeyName.ValueMember = "EmailContentKeyID";

				cklbTmp.DataSource = dsTmp.Tables[0];
				cklbTmp.DisplayMember = "EmailTemplet_Name";
				cklbTmp.ValueMember = "EmailContentKeyID";
			}
		}

		private bool Check()
		{
			DateTime dtStart = new DateTime();
			DateTime dtEnd = new DateTime();

			dtStart = dtpStart.Value;
			dtEnd = dtpEnd.Value;

			if (txtTitle.Text.Trim() == "")
			{
				MessageBox.Show("規則標題は空白不可!", "ECC Information");
				return false;
			}

			if (txtContent.Text.Trim() == "")
			{
				MessageBox.Show("規則内容は空白不可!", "ECC Information");
				return false;
			}

			if (!clsUICheck.CheckLen(txtTitle.Text.Trim(),50))
			{
				MessageBox.Show("規則標題入力長さは長すぎ!", "ECC Information");
				return false;
			}

			if (dtStart > dtEnd && ckbRule.Checked == true)
			{
				MessageBox.Show("終了日付は開始日付より遅い！", "ECC Information");
				return false;
			}


			for (int i = 0; i < dtvUser.Rows.Count; i++)
			{
				if (intMode == 0)
				{
					if (txtTitle.Text.Trim() == dtvUser.Rows[i].Cells["Rule_caption"].Value.ToString())
					{
						MessageBox.Show("規則標題重複!", "ECC Information");
						return false;
					}
				}
				else
				{
					if (txtTitle.Text.Trim() == dtvUser.Rows[i].Cells["Rule_caption"].Value.ToString() && i != dtvUser.CurrentRow.Index)
					{
						MessageBox.Show("規則標題重複!", "ECC Information");
						return false;
					}
				}
			}

			return true;
		}

		#region 
		//private void btnAdd_Click(object sender, EventArgs e)
		//{
		//    //for (int i = 0; i < objAL.Count; i++)
		//    //{
		//    //    string strNetSend = "net send " + objAL[i].ToString() + 
		//    //                        " you will OverTime from " + 
		//    //                        dtpStart.Value.ToLongTimeString() + " to " + dtpEnd.Value.ToLongTimeString();

		//    //    Process process = new Process();//创建进程对象
		//    //    ProcessStartInfo startinfo = new ProcessStartInfo();//创建进程时使用的一组值，如下面的属性
		//    //    startinfo.FileName = "cmd.exe";//设定需要执行的命令程序... //以下是隐藏cmd窗口的方法
		//    //    startinfo.Arguments = "/c" + strNetSend;//设定参数，要输入到命令程序的字符，其中“/c"表示执行完命令后马上退出
		//    //    startinfo.UseShellExecute = false;//不使用系统外壳程序启动
		//    //    startinfo.RedirectStandardInput = false;//不重定向输入
		//    //    startinfo.RedirectStandardOutput = true;//重定向输出，而不是默认的显示在dos控制台上
		//    //    startinfo.CreateNoWindow = true;//不创建窗口
		//    //    process.StartInfo = startinfo;
		//    //    try
		//    //    {
		//    //        if (process.Start() == false)//开始进程
		//    //        {
		//    //        }
		//    //        else
		//    //        {
		//    //            objAL.RemoveAt(i);
		//    //            objALName.RemoveAt(i);
		//    //        }
		//    //    }
		//    //    catch (Exception ex)
		//    //    {

		//    //    }
		//    //    finally
		//    //    {
		//    //        if (process != null)
		//    //        {
		//    //            process.Close();
		//    //        }
		//    //    }

		//    //}
		//    //string str = "";
		//    //for (int j = 0; j < objALName.Count; j++)
		//    //{
		//    //    str = str + objALName[j].ToString() + ";";
		//    //}

		//    //if (str.Length > 0)
		//    //{
		//    //    MessageBox.Show("用户" + str + " 消息发送失败", "ECC Information");
		//    //}

		//    BindData();
		//}
		#endregion

		private void BindDataByWhere(string strWhere)
		{
			BindCheckView();

			ClsRule obj = new ClsRule();
			DataSet dt = (DataSet)obj.GetList(strWhere);
			dtvUser.DataSource = null;
			if (dt.Tables.Count > 0)
			{
                

				this.btnDelete.Enabled = true;
				this.btnUpdata.Enabled = true;
				this.dtvUser.Enabled = true;
				this.btnAdd.Enabled = true;
				this.btnCancel.Enabled = false;
				this.btnSave.Enabled = false;
			}
			else
			{
				this.btnDelete.Enabled = false;
				this.btnUpdata.Enabled = false;
				this.dtvUser.Enabled = true;
				this.btnAdd.Enabled = true;
				this.btnCancel.Enabled = false;
				this.btnSave.Enabled = false;
			}

			if (dt.Tables.Count == 0)
			{
				return;
			}
			dtvUser.AutoGenerateColumns = false;
            
			dtvUser.DataSource = dt.Tables[0];

			if (dtvUser.Rows.Count > 0)
			{
				dtvUser.Rows[0].Selected = true;
				if ((bool)dtvUser.SelectedRows[0].Cells["Rule_TimeLimited"].Value == true)
				{
					dtpStart.Value = Convert.ToDateTime(dtvUser.Rows[0].Cells["Rule_TimeLimitedStartDate"].Value);
					dtpEnd.Value = Convert.ToDateTime(dtvUser.Rows[0].Cells["Rule_TimeLimitedEndDate"].Value);
					ckbRule.Checked = true;
				}
				else
				{
					dtpStart.Value = DateTime.Now;
					dtpEnd.Value = DateTime.Now;
					ckbRule.Checked = false;
				}

				for (int i = 0; i < cklbKeyName.Items.Count; i++)
				{
					DataRowView objDRV = (DataRowView)cklbKeyName.Items[i];
					if (Convert.ToString(dtvUser.Rows[0].Cells["Rule_KeyID"].Value) == objDRV.Row["Rule_KeyID"].ToString())
					{
						cklbKeyName.SetItemChecked(i, true);
						cklbTmp.SetItemChecked(i, true);
					}
					else
					{
						cklbKeyName.SetItemChecked(i, false);
						cklbTmp.SetItemChecked(i, false);
					}
				}
				txtOverTimeUser.Text = Convert.ToString(dtvUser.Rows[0].Cells["Rule_Member"].Value);
				txtOverTimeUser.Tag = Convert.ToString(dtvUser.Rows[0].Cells["Rule_MemberID"].Value);
				txtTitle.Text = Convert.ToString(dtvUser.Rows[0].Cells["Rule_caption"].Value);
				txtContent.Text = Convert.ToString(dtvUser.Rows[0].Cells["Rule_content"].Value);
				cmbCreatePerson.SelectedValue = dtvUser.Rows[0].Cells["Rule_CreatePerson"].Value;
				if (dtvUser.Rows[0].Cells["Rule_CreateDate"].Value != System.DBNull.Value)
				{
					dtpCreateDate.Value = Convert.ToDateTime(dtvUser.Rows[0].Cells["Rule_CreateDate"].Value);
				}
			}
		}

		private void BindData()
		{
			BindCheckView();

			ClsRule obj = new ClsRule();
			DataSet dt = (DataSet)obj.GetList();
			dtvUser.DataSource = null;
			if (dt.Tables.Count > 0)
			{
				this.btnDelete.Enabled = true;
				this.btnUpdata.Enabled = true;
				this.dtvUser.Enabled = true;
				this.btnAdd.Enabled = true;
				this.btnCancel.Enabled = false;
				this.btnSave.Enabled = false;
			}
			else
			{
				this.btnDelete.Enabled = false;
				this.btnUpdata.Enabled = false;
				this.dtvUser.Enabled = true;
				this.btnAdd.Enabled = true;
				this.btnCancel.Enabled = false;
				this.btnSave.Enabled = false;
			}

			if (dt.Tables.Count == 0)
			{
				return;
			}
			dtvUser.AutoGenerateColumns = false;
			dtvUser.DataSource = dt.Tables[0];

			if (dtvUser.Rows.Count > 0)
			{
				if ((bool)dtvUser.SelectedRows[0].Cells["Rule_TimeLimited"].Value == true)
				{
					dtpStart.Value = Convert.ToDateTime(dtvUser.Rows[0].Cells["Rule_TimeLimitedStartDate"].Value);
					dtpEnd.Value = Convert.ToDateTime(dtvUser.Rows[0].Cells["Rule_TimeLimitedEndDate"].Value);
					ckbRule.Checked = true;
				}
				else
				{
					dtpStart.Value = DateTime.Now;
					dtpEnd.Value = DateTime.Now;
					ckbRule.Checked = false;
				}

				for (int i = 0; i < cklbKeyName.Items.Count; i++)
				{
					DataRowView objDRV = (DataRowView)cklbKeyName.Items[i];
					if (Convert.ToString(dtvUser.Rows[0].Cells["Rule_KeyID"].Value) == objDRV.Row["Rule_KeyID"].ToString())
					{
						cklbKeyName.SetItemChecked(i, true);
						cklbTmp.SetItemChecked(i, true);
					}
					else
					{
						cklbKeyName.SetItemChecked(i, false);
						cklbTmp.SetItemChecked(i, false);
					}
				}
				txtOverTimeUser.Text = Convert.ToString(dtvUser.Rows[0].Cells["Rule_Member"].Value);
				txtOverTimeUser.Tag = Convert.ToString(dtvUser.Rows[0].Cells["Rule_MemberID"].Value);
				txtTitle.Text = Convert.ToString(dtvUser.Rows[0].Cells["Rule_caption"].Value);
				txtContent.Text = Convert.ToString(dtvUser.Rows[0].Cells["Rule_content"].Value);
				cmbCreatePerson.SelectedValue = dtvUser.Rows[0].Cells["Rule_CreatePerson"].Value;
				if (dtvUser.Rows[0].Cells["Rule_CreateDate"].Value != System.DBNull.Value)
				{
					dtpCreateDate.Value = Convert.ToDateTime(dtvUser.Rows[0].Cells["Rule_CreateDate"].Value);
				}
			}


		}

		private void frmRule_FormClosed(object sender, FormClosedEventArgs e)
		{
			if (objArrayList == null && strCaption == null)
			{
				Form frmMain = new frmMain();
				frmMain.Show();
			}
		}

		private void btnAddUser_Click(object sender, EventArgs e)
		{
			DigUser objForm = new DigUser();
			if (txtOverTimeUser.Text != "")
			{
				objForm.PersonID = Convert.ToString(txtOverTimeUser.Tag);
				objForm.PersonName = txtOverTimeUser.Text;
			}

			objForm.ShowDialog(this);
			strUserName = objForm.PersonName;
			strUserID = objForm.PersonID;
			strUserIP = objForm.PersonIP;
			this.txtOverTimeUser.Text = strUserName;
			txtOverTimeUser.Tag = strUserID;
		}

		private void btnSave_Click(object sender, EventArgs e)
		{

			ClsRule obj = new ClsRule();
			//string strUser = "";

			if (Check() == false)
			{
				return;
			}
			int intLimited;
			if (ckbRule.Checked == false)
			{
				intLimited = 0;
			}
			else
			{
				intLimited = 1;
			}
			
			string strRet = "";
			if (intMode == 0)
			{
				string[] strKeyID = Add();
				strRet = obj.AddRule(txtTitle.Text.ToString(), txtContent.Text.ToString(),intLimited,dtpStart.Value,dtpEnd.Value,
							txtOverTimeUser.Text.Trim(), CheckClass.clsUICheck.NullToString(txtOverTimeUser.Tag), ClsCommon.gIntOperatorID, strKeyID);
			}
			else
			{
				string[] strFlg = new string[this.cklbTmp.Items.Count]; ;
				string[] strKeyID = Updata(ref strFlg);
				strRet = obj.UpdateRule(dtvUser.SelectedRows[0].Cells["Rule_KeyID"].Value.ToString(), txtTitle.Text.ToString(), txtContent.Text.ToString(),
					intLimited, ClsCommon.gIntOperatorID, dtpStart.Value, dtpEnd.Value, txtOverTimeUser.Text.Trim(), CheckClass.clsUICheck.NullToString(txtOverTimeUser.Tag), strKeyID, strFlg);
				intMode = 0;
			}

			if (strRet == "1")
			{
				MessageBox.Show("データ失敗!", "ECC Information");
			}
			else if (strRet != "")
			{
				MessageBox.Show(strRet, "ECC Catch Error");
			}
			SetMessage();
			this.btnDelete.Enabled = true;
			this.btnUpdata.Enabled = true;
			//this.btnSave.Text = "追加";
			this.dtvUser.Enabled = true;
			this.txtOverTimeUser.Text = "";
			this.txtTitle.Text = "";
			this.txtContent.Text = "";
			this.BindData();
			ResetCheckList();
			ResetDateTime();
		}

		private string[] Add()
		{
			string[] strKeyID = new string[this.cklbKeyName.CheckedItems.Count];
			for (int i = 0; i < cklbKeyName.CheckedItems.Count; i++)
			{
				DataRowView objDRV = (DataRowView)cklbKeyName.CheckedItems[i];

				strKeyID[i] = objDRV.Row["EmailContentKeyID"].ToString();
			}

			return strKeyID;
		}

		private string[] Updata(ref string[] strFlg)
		{
			string[] strKeyID = new string[this.cklbTmp.Items.Count];
			int j = 0;
			for (int i = 0; i < cklbKeyName.Items.Count; i++)
			{
				if (cklbTmp.GetItemChecked(i) != cklbKeyName.GetItemChecked(i))
				{
					DataRowView objDRV = (DataRowView)cklbKeyName.Items[i];
					if (cklbKeyName.GetItemChecked(i) == true)
					{
						strKeyID[j] = objDRV.Row["EmailContentKeyID"].ToString();
						strFlg[j] = "";
						j++;
					}
					else
					{
						strKeyID[j] = null;
						strFlg[j] = null;
						j++;
					}
				}
				//if (cklbTmp.CheckedItems.Count > 0)
				//{
				//    bool blFlg = cklbTmp.GetItemChecked(i);

				//    if (cklbKeyName.GetItemChecked(i) != blFlg && cklbKeyName.GetItemChecked(i) == true)
				//    {
				//        DataRowView objDRV = (DataRowView)cklbKeyName.Items[i];

				//        strKeyID[j] = objDRV.Row["EmailContentKeyID"].ToString();
				//        strFlg[j] = "";
				//        j++;
				//    }
				//    else if (cklbKeyName.GetItemChecked(i) == blFlg && cklbKeyName.GetItemChecked(i) == true)
				//    {
				//        DataRowView objDRV = (DataRowView)cklbKeyName.Items[i];

				//        strKeyID[j] = objDRV.Row["EmailContentKeyID"].ToString();
				//        strFlg[j] = "";
				//        j++;
				//    }
				//    else if (cklbKeyName.GetItemChecked(i) != blFlg && cklbKeyName.GetItemChecked(i) == false)
				//    {
				//        strKeyID[j] = null;
				//        strFlg[j] = null;
				//        j++;
				//    }
				//}
				//else
				//{
				//    if (cklbKeyName.GetItemChecked(i) == true)
				//    {
				//        DataRowView objDRV = (DataRowView)cklbKeyName.Items[i];

				//        strKeyID[j] = objDRV.Row["EmailContentKeyID"].ToString();
				//        strFlg[j] = "";
				//        j++;
				//    }
				//}
			}

			return strKeyID;
		}

		private void btnDelete_Click(object sender, EventArgs e)
		{
			//wether have selected row,if have not selected row, pop-up prompt
			if (dtvUser.CurrentRow == null)
			{
				MessageBox.Show("データを選んでください！", "ECC Information");
				return;
			}
			else
			{
				if (MessageBox.Show("削除します、よろしいですか?", "ECC Information", MessageBoxButtons.OKCancel) == DialogResult.OK)
				{
					ECCSystem.BizRule.ClsRule objBRMC = new ECCSystem.BizRule.ClsRule();
					objBRMC.DeleteRule(Convert.ToString(dtvUser.SelectedRows[0].Cells["Rule_KeyID"].Value));
					this.BindData();
					txtOverTimeUser.Text = "";
					txtOverTimeUser.Tag = "";
					txtTitle.Text = "";
					txtContent.Text = "";
				}
			}
			ResetCheckList();
			ResetDateTime();
		}

		private void btnUpdata_Click(object sender, EventArgs e)
		{
			if (dtvUser.CurrentRow == null)
			{
				MessageBox.Show("データを選んでください！", "ECC Information");
				return;
			}
			
			//if ((bool)dtvUser.SelectedRows[0].Cells["Rule_TimeLimited"].Value == true)
			//{
			//    dtpStart.Value = Convert.ToDateTime(dtvUser.SelectedRows[0].Cells["Rule_TimeLimitedStartDate"].Value);
			//    dtpEnd.Value = Convert.ToDateTime(dtvUser.SelectedRows[0].Cells["Rule_TimeLimitedEndDate"].Value);
			//    ckbRule.Checked = true;
			//}
			//else
			//{
			//    dtpStart.Value = DateTime.Now;
			//    dtpEnd.Value = DateTime.Now;
			//    ckbRule.Checked = false;
			//}

			//for (int i = 0; i < cklbKeyName.Items.Count; i++)
			//{
			//    DataRowView objDRV = (DataRowView)cklbKeyName.Items[i];
			//    if (Convert.ToString(dtvUser.SelectedRows[0].Cells["Rule_KeyID"].Value) == objDRV.Row["Rule_KeyID"].ToString())
			//    {
			//        cklbKeyName.SetItemChecked(i, true);
			//        cklbTmp.SetItemChecked(i, true);
			//    }
			//}
			//txtOverTimeUser.Text = Convert.ToString(dtvUser.SelectedRows[0].Cells["Rule_Member"].Value);
			//txtOverTimeUser.Tag = Convert.ToString(dtvUser.SelectedRows[0].Cells["Rule_MemberID"].Value);
			//txtTitle.Text = Convert.ToString(dtvUser.SelectedRows[0].Cells["Rule_caption"].Value);
			//txtContent.Text = Convert.ToString(dtvUser.SelectedRows[0].Cells["Rule_content"].Value);
			this.btnDelete.Enabled = false;
			this.btnUpdata.Enabled = false;
			this.dtvUser.Enabled = false;
			this.btnAdd.Enabled = false;
			this.btnCancel.Enabled = true;
			this.btnSave.Enabled = true;
			//this.btnSave.Text = "保存";
			intMode = 1;
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			this.txtOverTimeUser.Text = "";
			this.txtTitle.Text = "";
			this.txtContent.Text = "";
			ResetCheckList();
			ResetDateTime();

			if (dtvUser.Rows.Count > 0)
			{
				this.btnDelete.Enabled = true;
				this.btnUpdata.Enabled = true;
				this.dtvUser.Enabled = true;
				this.btnAdd.Enabled = true;
				this.btnCancel.Enabled = false;
				this.btnSave.Enabled = false;

				dtvUser.Rows[0].Selected = true;
				if ((bool)dtvUser.CurrentRow.Cells["Rule_TimeLimited"].Value == true)
				{
					dtpStart.Value = Convert.ToDateTime(dtvUser.CurrentRow.Cells["Rule_TimeLimitedStartDate"].Value);
					dtpEnd.Value = Convert.ToDateTime(dtvUser.CurrentRow.Cells["Rule_TimeLimitedEndDate"].Value);
					ckbRule.Checked = true;
				}
				else
				{
					dtpStart.Value = DateTime.Now;
					dtpEnd.Value = DateTime.Now;
					ckbRule.Checked = false;
				}

				for (int i = 0; i < cklbKeyName.Items.Count; i++)
				{
					DataRowView objDRV = (DataRowView)cklbKeyName.Items[i];
					if (Convert.ToString(dtvUser.CurrentRow.Cells["Rule_KeyID"].Value) == objDRV.Row["Rule_KeyID"].ToString())
					{
						cklbKeyName.SetItemChecked(i, true);
						cklbTmp.SetItemChecked(i, true);
					}
					else
					{
						cklbKeyName.SetItemChecked(i, false);
						cklbTmp.SetItemChecked(i, false);
					}
				}
				txtOverTimeUser.Text = Convert.ToString(dtvUser.CurrentRow.Cells["Rule_Member"].Value);
				txtOverTimeUser.Tag = Convert.ToString(dtvUser.CurrentRow.Cells["Rule_MemberID"].Value);
				txtTitle.Text = Convert.ToString(dtvUser.CurrentRow.Cells["Rule_caption"].Value);
				txtContent.Text = Convert.ToString(dtvUser.CurrentRow.Cells["Rule_content"].Value);
				cmbCreatePerson.SelectedValue = dtvUser.SelectedRows[0].Cells["Rule_CreatePerson"].Value;
				if (dtvUser.SelectedRows[0].Cells["Rule_CreateDate"].Value != System.DBNull.Value)
				{
					dtpCreateDate.Value = Convert.ToDateTime(dtvUser.SelectedRows[0].Cells["Rule_CreateDate"].Value);
				}
			}
			else
			{
				this.btnDelete.Enabled = false;
				this.btnUpdata.Enabled = false;
				this.dtvUser.Enabled = true;
				this.btnAdd.Enabled = true;
				this.btnCancel.Enabled = false;
				this.btnSave.Enabled = false;
			}
		}

		private void ResetDateTime()
		{
			dtpStart.Value = DateTime.Now;
			dtpEnd.Value = DateTime.Now;
			ckbRule.Checked = false;
		}

		private void ResetCheckList()
		{
			for (int i = 0; i < cklbKeyName.Items.Count; i++ )
			{
				cklbKeyName.SetItemChecked(i, false);
				cklbTmp.SetItemChecked(i, false);
			}
		}

		private void SetMessage()
		{

			//tc.Connect(this.txtRempoteIP.Text.Trim(), 1980);
			ClsUserMaintenance objClsUserMaintenance = new ClsUserMaintenance();

			DataSet objDataSet = (DataSet)objClsUserMaintenance.GetList();

			string strIP = "";
			string[] strName = txtOverTimeUser.Text.Trim().Split(Convert.ToChar("|"));
			string strError = "";
			if (txtOverTimeUser.Text.Trim() != "")
			{
				for (int i = 0; i < strName.Length - 1; i++)
				{
					TcpClient tc = new TcpClient();
					try
					{
						byte[] buffer;

						if (objDataSet.Tables.Count > 0 && objDataSet.Tables[0].Rows.Count > 0)
						{
							DataView objDataView = objDataSet.Tables[0].DefaultView;
							objDataView.RowFilter = "UserName = '" + strName[i] + "' and DeleteFlag = 0";
							if (objDataView.Count > 0)
							{
								strIP = objDataView[0]["IP"].ToString();
								if (strIP.Trim() == "")
								{
									//strError = strError + "用户:" + strName[i] + "消息发送失败!" + Environment.NewLine;
								}
								else
								{
									tc.Connect(strIP, 1980);
									NetworkStream ns = tc.GetStream();
									buffer = System.Text.ASCIIEncoding.UTF8.GetBytes("1");
									ns.Write(buffer, 0, buffer.Length);
								}


								
								tc.Close();
							}
							else
							{
								strError = strError + "ユーザ:" + strName[i] + "のメッセージ発送失敗しました!" + Environment.NewLine;
							}
						}
					}
					catch
					{
						strError = strError + "ユーザ:" + strName[i] + "のメッセージ発送失敗しました!" + Environment.NewLine;
						
						tc.Close();
						
					}
					finally
					{
						
						tc.Close();
					}
				}
			}
			else
			{
				
				DataView objDataView = new DataView();
					byte[] buffer;

					if (objDataSet.Tables.Count > 0 && objDataSet.Tables[0].Rows.Count > 0)
					{
						objDataView = objDataSet.Tables[0].DefaultView;
						objDataView.RowFilter = "DeleteFlag = 0";
						if (objDataView.Count > 0)
						{
							for (int i = 0; i < objDataView.Count; i++)
							{
								TcpClient tc = new TcpClient();
								try
								{
									strIP = objDataView[i]["IP"].ToString();
									if (strIP.Trim() == "")
									{
										//strError = strError + "用户:" + objDataView[0]["UserName"].ToString() + "消息发送失败!" + Environment.NewLine;
									}
									else
									{
										tc.Connect(strIP, 1980);
										NetworkStream ns = tc.GetStream();
										buffer = System.Text.ASCIIEncoding.UTF8.GetBytes("1");
										ns.Write(buffer, 0, buffer.Length);
									}


									tc.Close();
								}
								catch
								{
									strError = strError + "ユーザ:" + objDataView[0]["UserName"].ToString() + "のメッセージ発送失敗しました!" + Environment.NewLine;

									tc.Close();
								}
								finally
								{

									tc.Close();
								}

						}
					}
				}

			}
			

			if (strError != "")
			{
				MessageBox.Show(strError, "ECC Information");
			}
				
		}

		private void dtvUser_CellClick(object sender, DataGridViewCellEventArgs e)
		{
			txtOverTimeUser.Text = "";
			txtOverTimeUser.Tag = "";
			txtTitle.Text = "";
			txtContent.Text = "";
			dtvUser.Rows[e.RowIndex].Selected = true;
			if (dtvUser.SelectedRows.Count <= 0)
			{
				return;
			}
            if (dtvUser.CurrentRow.Index <= 0)
            {
                return;
            }
			if ((bool)dtvUser.SelectedRows[0].Cells["Rule_TimeLimited"].Value == true)
			{
				dtpStart.Value = Convert.ToDateTime(dtvUser.SelectedRows[0].Cells["Rule_TimeLimitedStartDate"].Value);
				dtpEnd.Value = Convert.ToDateTime(dtvUser.SelectedRows[0].Cells["Rule_TimeLimitedEndDate"].Value);
				ckbRule.Checked = true;
			}
			else
			{
				dtpStart.Value = DateTime.Now;
				dtpEnd.Value = DateTime.Now;
				ckbRule.Checked = false;
			}

			for (int i = 0; i < cklbKeyName.Items.Count; i++)
			{
				DataRowView objDRV = (DataRowView)cklbKeyName.Items[i];
				if (Convert.ToString(dtvUser.SelectedRows[0].Cells["Rule_KeyID"].Value) == objDRV.Row["Rule_KeyID"].ToString())
				{
					cklbKeyName.SetItemChecked(i, true);
					cklbTmp.SetItemChecked(i, true);
				}
				else
				{
					cklbKeyName.SetItemChecked(i, false);
					cklbTmp.SetItemChecked(i, false);
				}
			}
			txtOverTimeUser.Text = Convert.ToString(dtvUser.SelectedRows[0].Cells["Rule_Member"].Value);
			txtOverTimeUser.Tag = Convert.ToString(dtvUser.SelectedRows[0].Cells["Rule_MemberID"].Value);
			txtTitle.Text = Convert.ToString(dtvUser.SelectedRows[0].Cells["Rule_caption"].Value);
			txtContent.Text = Convert.ToString(dtvUser.SelectedRows[0].Cells["Rule_content"].Value);
			cmbCreatePerson.SelectedValue = dtvUser.SelectedRows[0].Cells["Rule_CreatePerson"].Value;
			if (dtvUser.SelectedRows[0].Cells["Rule_CreateDate"].Value != System.DBNull.Value)
			{
				dtpCreateDate.Value = Convert.ToDateTime(dtvUser.SelectedRows[0].Cells["Rule_CreateDate"].Value);
			}
			
		}

		private void btnAdd_Click(object sender, EventArgs e)
		{
			this.txtOverTimeUser.Text = "";
			this.txtTitle.Text = "";
			this.txtContent.Text = "";

			this.btnDelete.Enabled = false;
			this.btnUpdata.Enabled = false;
			this.dtvUser.Enabled = false;
			this.btnAdd.Enabled = false;
			this.btnCancel.Enabled = true;
			this.btnSave.Enabled = true;

			for (int i = 0; i < cklbKeyName.Items.Count; i++)
			{
				cklbKeyName.SetItemChecked(i, false);
				cklbTmp.SetItemChecked(i, false);
			}

			intMode = 0;
		}

		private void btnSearch_Click(object sender, EventArgs e)
		{
			string strWhere = "";
			if (cmbSearchPerson.Text.Trim() == "" && dtpSearchTimeStart.Checked == false
				&& dtpSearchTimeEnd.Checked == false && txtSearchContent.Text.Trim() == "")
			{
				MessageBox.Show("検索条件を入力してください", "ECC Information");
				return;
			}

			if (cmbSearchPerson.Text.Trim() != "")
			{
				strWhere = "Rule_CreatePerson = '" + cmbSearchPerson.SelectedValue + "' ";
			}

			if (dtpSearchTimeStart.Checked == true && strWhere != "")
			{
				strWhere = strWhere + " and Rule_CreateDate >= '" + dtpSearchTimeStart.Value +
					"' and Rule_CreateDate <= '" + dtpSearchTimeEnd.Value + "' ";

			}
			else if (dtpSearchTimeStart.Checked == true && strWhere == "")
			{
				strWhere = strWhere + " Rule_CreateDate >= '" + dtpSearchTimeStart.Value +
					"' and Rule_CreateDate <= '" + dtpSearchTimeEnd.Value + "' ";
			}

			if (txtSearchContent.Text.Trim() != "" && strWhere != "")
			{
				strWhere = strWhere + " and Rule_content like '%" + txtSearchContent.Text.Trim() + "%'";

			}
			else if (txtSearchContent.Text.Trim() != "" && strWhere == "")
			{
				strWhere = strWhere + " Rule_content like '%" + txtSearchContent.Text.Trim() + "%'";
			}

			strWhere = strWhere + " And Rule_caption <> ' '";
			BindDataByWhere(strWhere);

		}

		private void dtpSearchTimeEnd_ValueChanged(object sender, EventArgs e)
		{
			if (dtpSearchTimeEnd.Checked == true)
			{
				dtpSearchTimeStart.Checked = true;
			}
			else
			{
				dtpSearchTimeStart.Checked = false;
			}

		}

		private void dtpSearchTimeStart_ValueChanged(object sender, EventArgs e)
		{
			if (dtpSearchTimeStart.Checked == true)
			{
				dtpSearchTimeEnd.Checked = true;
			}
			else
			{
				dtpSearchTimeEnd.Checked = false;
			}
		}
	}
}